package com.example.shouybl.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.shouybl.BaseApp;
import com.example.shouybl.R;
import com.example.shouybl.adapter.ActivityCommentAdapter;
import com.example.shouybl.bean.ActivityComment;
import com.example.shouybl.bean.AppBaseBean;
import com.example.shouybl.constant.Path;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

/**
 * 赚钱详情，评论
 * A simple {@link Fragment} subclass.
 */
public class CommentFragment extends Fragment {

    private android.widget.ListView lvgamecommemt;
    private ArrayList<ActivityComment> comments=new ArrayList<>();
    private ActivityCommentAdapter commentAdapter;
    private AsyncHttpClient client=new AsyncHttpClient();
    private String id="";
    private com.handmark.pulltorefresh.library.PullToRefreshListView ptrmoneycomment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_comment, container, false);


        initView(view);
        initData();
       /* ptrmoneycomment.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                initData();
            }
        });*/
        return view;
    }

    private void initData() {
        client.post(Path.PATH_MONEY_COMMENT, getParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<AppBaseBean<ActivityComment>> typeReference=new TypeReference<AppBaseBean<ActivityComment>>(){};
                AppBaseBean baseBean= JSONObject.parseObject(responseString,typeReference);
                if (baseBean!=null && baseBean.getState().equals("success")){
                    comments.addAll(baseBean.getInfo());
                }
                commentAdapter.notifyDataSetChanged();
            }
        });

    }

    private RequestParams getParams() {
        RequestParams params=new RequestParams();
        params.put("id",id);
        params.put("compare","df0886fe7abe42326f67992c59d2e8ae");
        return params;
    }

    private void initView(View view) {
        id=getArguments().getString("id");
       // this.ptrmoneycomment = (PullToRefreshListView) view.findViewById(R.id.ptr_money_comment);
        //lvgamecommemt=ptrmoneycomment.getRefreshableView();
        //ptrmoneycomment.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        this.lvgamecommemt = (ListView) view.findViewById(R.id.lv_game_commemt);
        commentAdapter=new ActivityCommentAdapter(comments, BaseApp.getInstance());
        lvgamecommemt.setAdapter(commentAdapter);
    }

}
